package com.yuanlrc.base.controller.admin;


import com.yuanlrc.base.bean.CodeMsg;
import com.yuanlrc.base.bean.PageBean;
import com.yuanlrc.base.bean.Result;
import com.yuanlrc.base.entity.admin.Faculties;
import com.yuanlrc.base.service.admin.FacultiesService;
import com.yuanlrc.base.service.admin.OperaterLogService;
import com.yuanlrc.base.util.ValidateEntityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * 学院列表管理控制器
 */
@Controller
@RequestMapping("/faculties")
public class FacultiesController {

    private Logger log = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private FacultiesService facultiesService;

    @Autowired
    private OperaterLogService operaterLogService;

    /**
     * 学院列表页面
     * @param model
     * @param faculties
     * @param pageBean
     * @return
     */
    @RequestMapping(value="/list")
    public String list(Model model, Faculties faculties, PageBean<Faculties> pageBean){
        model.addAttribute("title", "学院列表");
        model.addAttribute("pageBean", facultiesService.findList(faculties, pageBean));
        return "admin/faculties/list";
    }

    /**
     * 新增学院列表页面
     * @return
     */
    @RequestMapping(value="/add",method= RequestMethod.GET)
    public String add(){
        return "admin/faculties/add";
    }

    /**
     * 学院添加表单提交处理
     * @param faculties
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> add(Faculties faculties){
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(faculties);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        //判断学院编号是否存在
        if(facultiesService.isExistByNum(faculties.getNum())){
            return Result.error(CodeMsg.ADMIN_FACULTIESNUM_EXIST);
        }
        //判断学院名是否存在
        if(facultiesService.isExistByName(faculties.getName(), 0l)){
            return Result.error(CodeMsg.ADMIN_FACULTIESNAME_EXIST);
        }
        //到这说明一切符合条件，进行数据库新增
        if(facultiesService.save(faculties) == null){
            return Result.error(CodeMsg.ADMIN_FACULTIES_ADD_ERROR);
        }
        operaterLogService.add("添加学院，学院名：" + faculties.getName());
        return Result.success(true);
    }


    /**
     * 学院列表编辑页面
     * @param model
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.GET)
    public String edit(Model model,@RequestParam(name="id",required=true)Long id){
        model.addAttribute("faculties", facultiesService.find(id));
        return "admin/faculties/edit";
    }

    /**
     * 编辑学院信息表单提交处理
     * @param faculties
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> edit(Faculties faculties){
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(faculties);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        if(faculties.getId() == null || faculties.getId().longValue() <= 0){
            return Result.error(CodeMsg.ADMIN_FACULTIES_NO_EXIST);
        }
        //判断学院名是否存在
        if(facultiesService.isExistByName(faculties.getName(), faculties.getId())){
            return Result.error(CodeMsg.ADMIN_FACULTIESNAME_EXIST);
        }
        //到这说明一切符合条件，进行数据库保存
        Faculties findById = facultiesService.find(faculties.getId());
        //讲提交的用户信息指定字段复制到已存在的user对象中,该方法会覆盖新字段内容
        BeanUtils.copyProperties(faculties, findById, "id","createTime","updateTime");
        if(facultiesService.save(findById) == null){
            return Result.error(CodeMsg.ADMIN_FACULTIES_EDIT_ERROR);
        }
        operaterLogService.add("编辑学院，学院名：" + faculties.getName());
        return Result.success(true);
    }

    /**
     * 删除学院
     * @param id
     * @return
     */
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
        try {
            facultiesService.delete(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.ADMIN_FACULTIES_DELETE_ERROR);
        }
        log.info("编辑学院ID【"+id+"】");
        operaterLogService.add("删除学院ID【"+id+"】");
        return Result.success(true);
    }

}
